package cn.iocoder.yudao.module.blog.service.jiutag;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.jiutag.BlogJiuTagConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.jiutag.BlogJiuTagMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 案例标签分类 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogJiuTagServiceImpl implements BlogJiuTagService {

    @Resource
    private BlogJiuTagMapper jiuTagMapper;

    @Override
    public Long createJiuTag(BlogJiuTagCreateReqVO createReqVO) {

        BlogJiuTagDO blogJiuTagDO = jiuTagMapper.selectOne(new LambdaQueryWrapper<BlogJiuTagDO>()
                .eq(BlogJiuTagDO::getClassity, createReqVO.getClassity())
                .eq(BlogJiuTagDO::getName, createReqVO.getName()));
        if(blogJiuTagDO != null){
            throw exception(TAG_HAS_EXISTS);
        }

        // 插入
        BlogJiuTagDO jiuTag = BlogJiuTagConvert.INSTANCE.convert(createReqVO);
        jiuTagMapper.insert(jiuTag);
        // 返回
        return jiuTag.getId();
    }

    @Override
    public void updateJiuTag(BlogJiuTagUpdateReqVO updateReqVO) {
        // 校验存在
        validateJiuTagExists(updateReqVO.getId());
        // 更新
        BlogJiuTagDO updateObj = BlogJiuTagConvert.INSTANCE.convert(updateReqVO);
        jiuTagMapper.updateById(updateObj);
    }

    @Override
    public void deleteJiuTag(Long id) {
        // 校验存在
        validateJiuTagExists(id);
        // 删除
        jiuTagMapper.deleteById(id);
    }

    private void validateJiuTagExists(Long id) {
        if (jiuTagMapper.selectById(id) == null) {
            throw exception(JIU_TAG_NOT_EXISTS);
        }
    }

    @Override
    public BlogJiuTagDO getJiuTag(Long id) {
        return jiuTagMapper.selectById(id);
    }

    @Override
    public List<BlogJiuTagDO> getJiuTagList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return jiuTagMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogJiuTagDO> getJiuTagPage(BlogJiuTagPageReqVO pageReqVO) {
        return jiuTagMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogJiuTagDO> getJiuTagList(BlogJiuTagExportReqVO exportReqVO) {
        return jiuTagMapper.selectList(exportReqVO);
    }

    @Override
    public List<BlogJiuTagDO> getAllTags(){
        return jiuTagMapper.selectList();
    }

    @Override
    public List<BlogJiuTagDO> getHotTags() {
        return jiuTagMapper.selectList(BlogJiuTagDO::getHot, true);
    }
}
